Xceed Toolkit Plus for WPF v4.6 Documentation
Filtering Data
Welcome to Xceed Toolkit Plus for WPF v4.6 > DataGrid, ListBox, Chart, AvalonDock, and PropertyGrid > Listbox control > Filtering Data

The data contained in a listbox can be filtered (searched), programmatically or through end-user interaction, to display a subset of data. Programmatically, data can be filtered through the Filter event, which allows more complex filtering such as comparisons between two fields, by providing a filter expression (see FilterExpression class) via the FilterExpression property, or through a combination of both.  

End-User Filtering (Searching)

End-users can filter the data in a listbox by typing in the SearchTextBox. A search text box (and the FilterControl that contains it) is displayed in a tool pane by default, but is disabled. To hide the tool pane and therefore the search text box, set ToolPaneVisibility to Visibility.Collapsed. To activate the search text box, the FilteredFieldNames attached property must be set to a FieldNameList instance representing the fields by which the data can be filtered. The following XAML will allow the listbox to filter data by the Country field.

In order to use the search text box, you must include the Xceed.Wpf.Controls namespace in your application.
In order to display the search box (SearchTextBox), the ToolPaneVisibility property must be set to Visible.
<xclb:ListBox x:Name="listBox"
      ItemsSource="{Binding Path=Customers}"
      ItemTemplate="{StaticResource itemTemplate}">
    <xclb:SearchTextBox.FilteredFieldNames>
        <xclb:FieldNameList>
            <s:String>Country</s:String>
        </xclb:FieldNameList>
    </xclb:SearchTextBox.FilteredFieldNames>
</xclb:ListBox>

The SearchValueDelimiters property on the SearchTextBox class contains the characters that are accepted as delimiters between search values.

The FilterControl class acts as a delay-providing intermediary between the SearchTextBox and the ListBox. As the end-user types in the SearchTextBox, the UIFilterExpression property is updated continuously (being bound to SearchTextBox.FilterExpression through two-way binding in the product's default templates) to contain the filter that should be applied to the data. However, the FilterControl waits for the amount of time represented by ApplyFilterInterval to pass, without any other changes being made in the SearchTextBox, before DataSourceFilterExpression (bound to ListBox.FilterExpression in the product's default templates) is updated using UIFilterExpression. The advantage of this approach is to avoid the filter being applied after each keystroke. By default, the delay value is a TimeSpan representing 700 milliseconds. The FilterControl's ClearFilterButtonVisibility property allows the clear filter button to be hidden. The ClearFilterCommand is an ICommand representing the command associated with the clear filter button.

The SearchTextBox is part of the FilterControl control template, which is in turn part of the the ListBox control template.

FilterExpression Property

The filter expression, which is accessed through a listbox's FilterExpression property, is imbedded into the data query that is used to request data and filters the items before they are returned. The filter expression that is provided can be a simple, single-value expression (see FilterExpression class), or a more complex expression using one or more logical filter expressions (see AndFilterExpression, NotFilterExpression, and OrFilterExpression classes). Each filter expression defines a value, the name of the member by whose values the items are to be filtered, and the filter operator to use (see FilterOperator enumeration). If a logical filter expression is used, at least two operand filter expressions must be provided. 

<xclb:ListBox.FilterExpression>
   <xclb:AndFilterExpression>
      <xclb:FilterExpression MemberName="ShipVia"  
                             Value="3"
                             FilterOperator="NotEqual"/>
      <xclb:FilterExpression MemberName="ShipCountry"
                             Value="Canada"
                             FilterOperator="Equal"/>
   </xclb:AndFilterExpression>
</xclb:ListBox.FilterExpression>
listBox.FilterExpression = New FilterExpression( "ShipCity", FilterOperator.LessThan, "N" )
listBox.FilterExpression = new FilterExpression( "ShipCity", FilterOperator.LessThan, "N" );

Filter Event

The Filter event is raised for each item in the data source that has not already been excluded by a filter expression to determine if it passes a filter. Unlike filter expressions, the Filter event can be used to provide more complex filtering such as comparisons between two fields.

The Filter event is executed locally, meaning that all items will be retrieved from the server in order to determine those that pass the filter. In a virtualized environment, it is not recommended to use the Filter event unless dealing with a very limited number of items.
Private Sub ListBoxControl_Filter( ByVal sender As Object, ByVal e As ListBoxFilterEventArgs )
   Dim order As Order = CType( e.Item, Order )

   If order Is Nothing Then
      e.Accepted = False
   Else
      e.Accepted = ( order.ShippedDate > order.RequiredDate )
   End If
End Sub
private void ListBoxControl_Filter( object sender, ListBoxFilterEventArgs e )
{
   Order order = e.Item as Order;

   if( order == null )
   {
      e.Accepted = false;
   }
   else
   {
      e.Accepted = ( order.ShippedDate > order.RequiredDate );
   }
}